<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/7/5
 * Time: 9:45
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\cache\RecommendByKeyWord as cRecommendByKeyWord;
use Biz\model\stat\GoodsTag;
use Biz\model\trade\Cart as mCart;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\service\search\Goods as searchGoods;

$startTime = date("Y-m-d H:i:s", strtotime("-1 day"));
$endTime = date("Y-m-d H:i:s");

// 热门所需数据
$cartsql = "select count(*) as count ,goods_id from `cart` WHERE shop_id NOT IN (3192940,3657469,4787600) and  addtime > '$startTime' and addtime < '$endTime'   GROUP BY goods_id  ORDER BY count DESC LIMIT 0,30";
$cartData = mCart::query($cartsql)->fetchAll();
$cartList = [];
foreach ($cartData as $value) {
    $cartList[] = $value['goods_id'];
}


$orderGoodsSumSql = "select sum(pay_amount) as sums,goods_id,pay_amount from `order_goods` WHERE  shop_id NOT IN (3192940,3657469,4787600) and   addtime > '$startTime' and addtime < '$endTime' GROUP BY goods_id ORDER BY sums DESC LIMIT 0,10";
$orderGoodsSumData = mOrderGoods::query($orderGoodsSumSql)->fetchAll();
$orderGoodsSumList = [];
foreach ($orderGoodsSumData as $value) {
    $orderGoodsSumList[] = $value['goods_id'];
}


$orderGoodsCountSql = "select count(*) as count ,goods_id from `order_goods` WHERE shop_id NOT IN (3192940,3657469,4787600) and   addtime > '$startTime' and addtime < '$endTime' GROUP BY goods_id ORDER BY count DESC   LIMIT 0,10";
$orderGoodsCountData = mOrderGoods::query($orderGoodsCountSql)->fetchAll();
$orderGoodsCountList = [];
foreach ($orderGoodsCountData as $value) {
    $orderGoodsCountList[] = $value['goods_id'];
}

//爆款 畅销书籍
$date = date('Y-m-d');
$hotTagsGoods = GoodsTag::select("*", [
    'date' => $date,
    'tag' => [
        searchGoods::HEAT_TAG_EXPLOSION,
        searchGoods::HEAT_TAG_HOT
    ],
    'ORDER' => ['avg_sales' => 'DESC'],
    'LIMIT' => 200
]);
foreach ($hotTagsGoods as $value) {
    $hotTagsGoodsList[] = $value['goods_id'];
}


$hotGoods = array_slice(array_unique(array_merge($cartList, $orderGoodsSumList, $orderGoodsCountList, $hotTagsGoodsList)), 0, 200); // hot
if (!empty($hotGoods)) {
    cRecommendByKeyWord::delGoods('hot');
    $hotResult = cRecommendByKeyWord::setGoods('hot', $hotGoods);
}
if ($hotResult) {
    echo printf("%s,%s", date('Y-m-d H:i:s'), '热门商品同步成功');
}

// 上新所需数据
$date = date('Y-m-d');
$newGoods = GoodsTag::select("*", [
    'date' => $date,
    'tag' => searchGoods::HEAT_TAG_NEW,
    'ORDER' => ['days' => 'ASC'],
    'LIMIT' => 150
]);
$highExposureUnsalableGoods = GoodsTag::select("*", [
    'date' => $date,
    'tag' => searchGoods::HEAT_TAG_HIGH_EXPOSURE_UNSALABLE,
    'ORDER' => ['pv' => 'DESC'],
    'LIMIT' => 300 - count($newGoods)
]);
$newGoodsOffset = 0;
$highExposureUnsalableGoodsOffset = 0;
$mergeGoods = [];
do {
    if ($newGoodsOffset + 10 < count($newGoods)) {
        $mergeGoods = array_merge($mergeGoods, array_slice($newGoods, $newGoodsOffset, 10));
        $newGoodsOffset += 10;
        $mergeGoods = array_merge($mergeGoods, array_slice($highExposureUnsalableGoods, $highExposureUnsalableGoodsOffset, 10));
        $highExposureUnsalableGoodsOffset += 10;
    } elseif ($newGoodsOffset < count($newGoods)) {
        $mergeGoods = array_merge($mergeGoods, array_slice($newGoods, $newGoodsOffset));
        $mergeGoods = array_merge($mergeGoods, array_slice($highExposureUnsalableGoods, $highExposureUnsalableGoodsOffset));
        break;
    }
} while (true);
if (!empty($mergeGoods)) {
    $goodsIds = array_map(function ($goods) {
        return $goods['goods_id'];
    }, $mergeGoods);
    cRecommendByKeyWord::delGoods('new');
    $newResult = cRecommendByKeyWord::setGoods('new', $goodsIds);
}









